package com.qqtech.qquan.community.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.community.enums.CommunityMessageStatusDictEnum;
import com.qqtech.qquan.community.model.CommunityBaseVo;
import com.qqtech.qquan.community.model.CommunityMessage;
import com.qqtech.qquan.community.model.CommunityMessageQuery;
import com.qqtech.qquan.community.model.CommunityMessageVo;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.community.service.CommunityMessageService;
import com.qqtech.qquan.sys.controller.BaseAdminController;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserBaseService;

@Controller
@RequestMapping(value = "/community/message")
public class CommunityMessageController extends BaseAdminController {

	@Resource
	private CommunityMessageService communityMessageService;

	@Resource
	private UserBaseService userBaseService;

	@Resource
	private CommunityBaseService communityBaseService;

	@Override
	protected BaseService<CommunityMessage> getBaseService() {
		return communityMessageService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/community/communityMessage-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(CommunityMessageQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<CommunityMessageVo> page = communityMessageService.queryPageListAdmin(query, pageKit);
		UserBaseVo userBaseVo = null;
		CommunityBaseVo communityBaseVo = null;
		for (CommunityMessageVo communityMessageVo : page) {
			userBaseVo = userBaseService.getById(communityMessageVo.getCreatorId());
			communityMessageVo.setUserNick(userBaseVo.getNick());
			communityBaseVo = communityBaseService.getById(communityMessageVo.getCommunityId());
			communityMessageVo.setCommunityName(communityBaseVo.getName());
		}
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}

	/**
	 * 正常
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/receive")
	@ResponseBody
	public Result receive(Integer id) {
		Result ret = new Result();
		CommunityMessageVo communityMessageVo = new CommunityMessageVo();
		communityMessageVo.setId(id);
		communityMessageVo.setStatus(CommunityMessageStatusDictEnum.NORMAL.getCode());
		communityMessageVo.setIsShow(YesNoEnum.YES.getCode());
		communityMessageService.updateById(communityMessageVo);
		return ret;
	}

	/**
	 * 删除
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/unclaimed")
	@ResponseBody
	public Result unclaimed(Integer id) {
		Result ret = new Result();
		CommunityMessageVo communityMessageVo = new CommunityMessageVo();
		communityMessageVo.setId(id);
		communityMessageVo.setStatus(CommunityMessageStatusDictEnum.DELETE.getCode());
		communityMessageVo.setIsShow(YesNoEnum.NO.getCode());
		communityMessageService.updateById(communityMessageVo);
		return ret;
	}

	/**
	 * 置顶
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/doTop")
	@ResponseBody
	public Result doTop(Integer id) {
		Result ret = new Result();
		CommunityMessage message = new CommunityMessage();
		message.setId(id);
		message.setIsTop(YesNoEnum.YES.getCode());
		message.setTopAt(TimeUtil.now());
		int updateCount = communityMessageService.updateById(message);
		if (updateCount != 1) {
			ret.addErr("大喇叭-置顶失败");
		}
		return ret;
	}

	/**
	 * 取消置顶
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/undoTop")
	@ResponseBody
	public Result undoTop(Integer id) {
		Result ret = new Result();
		CommunityMessage message = new CommunityMessage();
		message.setId(id);
		message.setIsTop(YesNoEnum.NO.getCode());
		message.setTopAt(TimeUtil.now());
		int updateCount = communityMessageService.updateById(message);
		if (updateCount != 1) {
			ret.addErr("大喇叭-取消置顶失败");
		}
		return ret;
	}

	/**
	 * 取消官方
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/countermand")
	@ResponseBody
	public Result countermand(Integer id) {
		Result ret = new Result();
		CommunityMessageVo communityMessageVo = new CommunityMessageVo();
		communityMessageVo.setId(id);
		communityMessageVo.setIsOfficial(YesNoEnum.NO.getCode());
		communityMessageService.updateById(communityMessageVo);
		return ret;
	}
}
